from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

# 用户
class User(db.Model):
    __tablename__ = 'user'
    user_id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(255), unique=True, nullable=False)
    phone = db.Column(db.String(20))
    password = db.Column(db.String(255), nullable=False)
    created_at = db.Column(db.TIMESTAMP, default=datetime.utcnow)
    updated_at = db.Column(db.TIMESTAMP, default=datetime.utcnow, onupdate=datetime.utcnow)
    profile_picture = db.Column(db.Text)
    bio = db.Column(db.Text)
    preference = db.Column(db.Text)

    def __repr__(self):
        return f"<User {self.username}>"

# 饮食记录
class DietaryRecord(db.Model):
    __tablename__ = 'dietary_record'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    date = db.Column(db.Date, nullable=True)
    meal_type = db.Column(db.TEXT, nullable=True)
    food_item = db.Column(db.String(255), nullable=True)
    nutrition_info = db.Column(db.TEXT, nullable=True)  # 营养信息可能为空，因此设置为nullable=True
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 使用utcnow作为默认时间戳

    # 定义与User模型的关系
    user = db.relationship('User', backref=db.backref('dietary_records', lazy=True))

    def __init__(self, user_id, date, meal_type, food_item, nutrition_info):
        self.user_id = user_id
        self.date = date
        self.meal_type = meal_type
        self.food_item = food_item
        self.nutrition_info = nutrition_info
        self.created_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()

# 健康报告
class HealthReport(db.Model):
    __tablename__ = 'health_report'
    report_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    date = db.Column(db.Date, nullable=True)
    advice = db.Column(db.Text, nullable=True)

    def __init__(self, user_id, date, advice):
        self.user_id = user_id
        self.date = date
        self.advice = advice


# 食材管理
class IngredientsInventory(db.Model):
    __tablename__ = 'ingredients_inventory'
    
    inventory_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    ingredient_name = db.Column(db.String(255), nullable=False)
    quantity = db.Column(db.Numeric(10, 2), nullable=False)
    unit = db.Column(db.String(20), nullable=False)
    created_at = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp())
    updated_at = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
    storage_advice = db.Column(db.Text, nullable=True)
    location = db.Column(db.Text, nullable=True)

    def __init__(self, user_id, ingredient_name, quantity, unit, storage_advice=None, location=None):
        self.user_id = user_id
        self.ingredient_name = ingredient_name
        self.quantity = quantity
        self.unit = unit
        self.storage_advice = storage_advice
        self.location = location